package com.lucun.insurance.service.Impl;

import com.lucun.insurance.db.entity.Insure;
import com.lucun.insurance.db.mapper.InsureEntryChildMapper;
import com.lucun.insurance.db.mapper.InsureEntryStaffMapper;
import com.lucun.insurance.db.mapper.InsureMapper;
import com.lucun.insurance.service.InsureService;
import com.lucun.insurance.util.BaseException;
import com.lucun.insurance.util.JsonResult;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: 郭鑫
 * @catalogue: com.lucun.insurance.db.service.Impl
 * @Date: 2021/9/8 11:28
 * @Version: 1.0
 */
@Service
public class InsureServiceImpl implements InsureService {
    @Autowired
    InsureMapper insureMapper;
    @Autowired
    InsureEntryChildMapper insureEntryChildMapper;
    @Autowired
    InsureEntryStaffMapper insureEntryStaffMapper;

    //    添加被保人信息
    @Override
    public JsonResult<?> insert(Insure insure) {
        int sex = insure.getSex();
        if (ObjectUtils.isEmpty(sex)) {
            throw new BaseException("性别不能为空");
        }
        int cardType = insure.getCardType();
        if (ObjectUtils.isEmpty(cardType)) {
            throw new BaseException("证件类型不能为空");
        }
        int entryId = insure.getCardType();
        if (ObjectUtils.isEmpty(entryId)) {
            throw new BaseException("录入类型id不能为空");
        }
        String name = insure.getName();
        if (StringUtils.isBlank(name)) {
            throw new BaseException("被保人姓名不能为空");
        }
        String territory = insure.getTerritory();
        if (StringUtils.isBlank(territory)) {
            throw new BaseException("属地不能为空");
        }
        String phone = insure.getPhone();
        if (StringUtils.isBlank(phone)) {
            throw new BaseException("手机号不能为空");
        }
        String cardNumber = insure.getCardNumber();
        if (StringUtils.isBlank(cardNumber)) {
            throw new BaseException("证件号码不能为空");
        }
        Date birthday = insure.getBirthday();
        if (ObjectUtils.isEmpty(birthday)) {
            throw new BaseException("出生日期不能为空");
        }
        Date insureTime = insure.getInsureTime();
        if (ObjectUtils.isEmpty(insureTime)) {
            throw new BaseException("投保时间不能为空");
        }
        Date withdrawalTime = insure.getWithdrawalTime();
        if (ObjectUtils.isEmpty(withdrawalTime)) {
            throw new BaseException("终保时间不能为空");
        }
        insure.setCreateTime(new Date());
        int i = insureMapper.insert(insure);
        if (i != 0) {
            return new JsonResult<>("200");
        }
        return new JsonResult<>("500");
    }

    //查询所有
    @Override
    public List<Insure> findAll() {
        return insureMapper.findAll();
    }

    //    根据投保人id查询
    @Override
    public List<Insure> findById(Insure userId) {
        return insureMapper.findById(userId);
    }

    //删除
    @Override
    public JsonResult<?> delete(Insure insure) {
        Long id = insure.getId();
        if (ObjectUtils.isEmpty(id)) {
            throw new BaseException("没有获取此id");
        }
        int i = insureMapper.deleteByPrimaryKey(id);
        int j = insureEntryChildMapper.deleteById(id);
        int o = insureEntryStaffMapper.deleteById(id);

        if (i != 0) {
            if (j != 0) {
                if (o != 0) {
                    return new JsonResult<>("200");
                }
            }
        }
        return new JsonResult<>("500");
    }

    //修改
    @Override
    public JsonResult<?> update(Insure insure) {
        int i = insureMapper.updateByPrimaryKeySelective(insure);
        if (i != 0) {
            return new JsonResult<>("200");
        }
        return new JsonResult<>("500");
    }
}